package com.example.yy.fragementtest318yy.model;




import com.example.yy.fragementtest318yy.bean.VideoBean;
import com.example.yy.fragementtest318yy.iface.VideoListener;
import com.example.yy.fragementtest318yy.iface.Videoiface;
import com.example.yy.fragementtest318yy.service.VideoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yy on 2017/4/25.
 */

public class VideoModel implements Videoiface {
    //1
    private Retrofit retrofit;
    private String BASEURL =
            "http://amicool.neusoft.edu.cn/";

    public VideoModel() {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String video, int page, String sessionID, final VideoListener videoListener) {
        //2
        VideoService videoService=retrofit.create(VideoService.class);
        Call<List<VideoBean>> call=videoService.getResult(video,page,sessionID);
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    videoListener.onResponse(response.body());
                }
                else
                    videoListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                videoListener.onFail(t.toString());
            }
        });
    }
}

